import store from '@/store'
export default {
    namespaced: true,
    state: {
        pool: [],
        menuArr: []
    },
    actions: {
        async setMenuData({commit, state, dispatch},{to,from,that}){
                //let menu=state.page.menu
            console.log(state.menuArr)
            let path = to.path;
            let pathArr = path.split("/");
            let urlArr = [
                "air",
                "water",
                "pollution",
                "comprehensive",
                "warning",
                "daily",
                "knowledge",
                "management",
                "multidata",
            ];
            let isInPath = urlArr.includes(pathArr[1]);
            if (!isInPath) {

                return false;
            }
            let _component = pathArr[1];
            commit(
                "store/backgroup/setBackgroundUrl",
                _component,
                {root: true}
            );

            if (
                pathArr[1] == "pollution" ||
                pathArr[1] == "water" ||
                pathArr[1] == "air" ||
                pathArr[1] == "management"
            ) {
                this.menuChildren = false;
            }
            let _menu = [];
            let menuList=[];

            state.menuArr.forEach((item) => {
                if (item.name == pathArr[1]) {
                    /*if(this.$user.id == 1){
                        this.menuList = item.children;
                        return
                    }*/
                    item.children.forEach((v,index) => {
                        if (v.path == "index") {
                            _menu.push(v);
                        }
                        if (that.$menu.has("/" + _component + "/" + v.path)) {
                            _menu.push(v);
                            if(v.children&&v.children.length>0){
                                var childArr = [];
                                v.children.forEach((_v,_index)=>{
                                    if(that.$menu.has("/" + _component + "/" + v.path+"/"+_v.path)){
                                        childArr.push(_v)
                                    }
                                })
                            }
                            v.children = childArr;
                        }
                    });
                    this.menuList = _menu;
                }
            });
            console.log(menuList)
            /*this.menuList.forEach((item, index) => {
                if ("/" + this._component + "/" + item.path == path) {
                    this.openKeys = ["sub" + index];
                }
                if (item.children && item.children.length > 0) {
                    item.children.forEach((_item, _index) => {
                        if ("/" +this._component +"/" +item.path +"/" +_item.path ==path) {
                            this.openKeys = ["sub" + index];
                            this.openSelect = [_index];
                        }
                    });
                }
            });
            this.$forceUpdate();
            this.isMenu = true;*/
        },
    },
    mutations: {
        /**
         * @class pool
         * @description 保存 pool (候选池)
         * @param {Object} state state
         * @param {Array} routes routes
         */
        init(state, routes) {
            const pool = []
            const push = function (routes) {
                routes.forEach(route => {
                    if (route.children && route.children.length > 0) {
                        push(route.children)
                    } else {
                        if (!route.hidden) {
                            const {meta, name, path} = route
                            pool.push({meta, name, path})
                        }
                    }
                })
            }
            push(routes)
            state.pool = pool
            //console.log(pool)
        },
        menu(state, routes) {
            state.menuArr = routes
            console.log( state.menuArr)
        }
    }
}
